﻿using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;

namespace MinistryManager.DataAccess.Services.NHibernate
{
    public class NHibernateModel : INHibernateModel
    {
        #region Singleton members
        private static INHibernateModel _instance;

        private NHibernateModel()
        {
            var config = new Configuration();
            SessionFactory = config.Configure().BuildSessionFactory();
        }

        public static INHibernateModel GetInstance()
        {
            return _instance ?? (_instance = new NHibernateModel());
        }
        #endregion

        #region INHibernateModel
        public ISessionFactory SessionFactory { get; private set; }
        #endregion

        #region IDataModel members
        public IList<T> GetByCriterion<T>(List<ICriterion> criterion) where T : class
        {
            using (var session = SessionFactory.OpenSession())
            {
                var query = session.QueryOver<T>();
                foreach (var criteria in criterion)
                {
                    query.And(criteria);
                }
                var result = query.List();
                return result;
            }
        }

        public void InsertOrUpdate<T>(T target, int id) where T : class
        {
            throw new NotImplementedException();
        }

        public void InsertOrUpdate<T>(List<T> list) where T : class
        {
            throw new NotImplementedException();
        }

        public void Delete<T>(List<T> list) where T : class
        {
            throw new NotImplementedException();
        }

        public T GetById<T>(int id)
        {
            throw new NotImplementedException();
        }

        public IList<T> GetAll<T>() where T : class
        {
            throw new NotImplementedException();
        }

        public IList<T> GetAll<T>(string orderBy, bool asc) where T : class
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
